import pygame
import sys
import random

# 初始化Pygame
pygame.init()

# 设置窗口大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))

# 设置标题
pygame.display.set_caption("大富翁游戏")

# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
yellow = (255, 255, 0)

# 游戏参数
num_players = 2  # 玩家数量
dice_max = 6  # 骰子最大点数
board_size = 20  # 棋盘大小，这里假设为20格
property_names = ["起点"] + ["财产%d" % i for i in range(1, board_size)]  # 属性名列表

# 初始化玩家位置
player_positions = [0 for _ in range(num_players)]
player_colors = [red, green]


# 绘制游戏界面
def draw_game_interface():
    screen.fill(white)
    # 绘制棋盘边界
    pygame.draw.rect(screen, black, (50, 50, 700, 700), 5)
    # 绘制棋盘格子
    square_size = 700 // board_size
    for x in range(board_size):
        for y in range(board_size):
            pygame.draw.rect(screen, black, (50 + square_size * y, 50 + square_size * x, square_size, square_size), 1)

    # 绘制棋子
    for index, position in enumerate(player_positions):
        if position > 0:
            # 计算棋子的位置
            x_pos = position // board_size
            y_pos = position % board_size
            pygame.draw.circle(screen, player_colors[index],
                               (50 + square_size * (y_pos + 0.5), 50 + square_size * (x_pos + 0.5)), square_size // 4)


# 主循环
running = True
current_player = 0
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:  # 空格键投掷骰子
                dice_roll = random.randint(1, dice_max)
                print(f"Player {current_player + 1} rolled a {dice_roll}")
                player_positions[current_player] = (player_positions[current_player] + dice_roll) % (
                            board_size * board_size)
                current_player = (current_player + 1) % num_players

    draw_game_interface()
    pygame.display.flip()

# 清理并退出Pygame
pygame.quit()
sys.exit()